package tk3.project;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class PicShareActivity extends Activity {
	
	public int interval;
	public int radius;
	public String path;

	public static final int NOTIFICATION_ID = 1;
	
	private Button button1;
	private TextView textView1;
	private AlertDialog alertDialog;
	private NotificationManager notificationManager;
	private Notification newPicShareNotification1;
	private Notification newPicShareNotification2;
	
	private boolean start;
		
    /** Called when the activity is created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // settings update before service will be called
		SettingsActivity set = new SettingsActivity();
		set.setContext(getApplicationContext());
		set.setQS(true);
		set.load();
		//----------------------------------------------
        
        final Intent inte = new Intent(this, NewImageObservationService.class);
        
        textView1 = (TextView)findViewById(R.id.textView1);
        button1 = (Button)findViewById(R.id.button1);
        
        if(!isScanServiceRunning()){
        	textView1.setText("Click start to activate scan");
        	button1.setText("Start");
        	button1.setBackgroundColor(Color.BLACK);
        	button1.setBackgroundResource(R.drawable.play);
        }
        else{
        	textView1.setText("Scan active");
        	button1.setText("Stop");
        	button1.setBackgroundResource(R.drawable.stop);
        }
        
        button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(start == false){
					start = true;
					button1.setText("Stop");
					button1.setBackgroundResource(R.drawable.stop);
					textView1.setText("Scan active");
					startService(inte); 
					announceNewScan();
				}
				else {
					stopService(new Intent(v.getContext(), NewImageObservationService.class));
					start = false;
					button1.setText("Start");
					button1.setBackgroundResource(R.drawable.play);
					textView1.setText("Click start to activate scan");
					stopService(inte);
					announceNewScan();
				}
			}
		});
        
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Exit...");
        alertDialog.setMessage("Are You sure?");
        
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            	notificationManager.cancel(NOTIFICATION_ID);
            	stopService(inte);
            	finish();
            	System.exit(0);
          } });
        alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
          } });
        alertDialog.setIcon(R.drawable.exit);
        
        int icon1 = R.drawable.ico;
        int icon2 = R.drawable.icobw;
        String tickerText = "PicShare";
        long when = System.currentTimeMillis();
        newPicShareNotification1 = new Notification(icon1, tickerText, when);
        newPicShareNotification2 = new Notification(icon2, tickerText, when);
        announceNewScan();		
    }
    
    /** Checks if NewImageObservationService is running. */
    
    private boolean isScanServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("tk3.project.NewImageObservationService".equals(service.service.getClassName())) {
            	//System.out.println("Scan already running");
            	start = true;
                return true;
            }
        }
        start = false;
        return false;    
    }

    /** To show a notification when activity was started */
    
    private void announceNewScan() {
    	String svcName = Context.NOTIFICATION_SERVICE;
    	
    	notificationManager = (NotificationManager)getSystemService(svcName);
    	Context context = getApplicationContext();
    	String expandedText;
    	String expandedTitle = "PicShare";
    	Intent startActivityIntent = new Intent(this, PicShareActivity.class);
    	PendingIntent launchIntent = PendingIntent.getActivity(context, 0, startActivityIntent, 0);
    	if(isScanServiceRunning()){
    		expandedText = "Scan active";
    		newPicShareNotification1.setLatestEventInfo(context, expandedTitle, expandedText, launchIntent);
        	newPicShareNotification1.when = java.lang.System.currentTimeMillis();
        	notificationManager.notify(NOTIFICATION_ID, newPicShareNotification1);
    	}
    	else {
    		expandedText = "PicShare ready";
    		newPicShareNotification2.setLatestEventInfo(context, expandedTitle, expandedText, launchIntent);
        	newPicShareNotification2.when = java.lang.System.currentTimeMillis();
        	notificationManager.notify(NOTIFICATION_ID, newPicShareNotification2);
    	}
    }
    
    /* Methods for option-bar */
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
   
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.exit:     
            	alertDialog.show();
                break;
            case R.id.settings:   
            	Intent intent;
            	intent = new Intent(getApplicationContext(), SettingsActivity.class);
            	startActivity(intent);
                break;
        }
        return true;
    }
    
	protected void onStop() {
		super.onStop();
	}
	
	protected void onPause() {
        super.onPause();
    }
	
	public void onBackPressed(){
		super.onBackPressed();
	}
}